    //
//  WeiboDetailController.m
//  WeiboSpace
//
//  Created by  linan4 on 12-3-5.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "WeiboDetailController.h"
#import "ImageLoaderUtility.h"
@implementation WeiboDetailController
@synthesize status;

- (id)InitViewWithStatus:(NSDictionary*)message {
	
		if (self = [super init]) 
		{
		  self.status = message;
		}
		return self;
	}

- (void)viewDidUnload {
	self.status = nil;
    [super viewDidUnload];
}

//tag
#define HEAD_TAG 01
#define NICK_TAG 02
#define STATUS_TAG 01
//rect
#define BORDER_SIZE 7 
#define HEAD_SIZE	30
#define NICK_WIDTH	200
#define NICK_HEIGHT	15
#define GAP1_SIZE	7
#define THUMB_LEFT	25
#define THUMB_SIZE	320-16*GAP1_SIZE
#define  BUTTON_HEIGHT 80
#define TIME_HEIGHT 8
#define STATUS_YOFFSET BORDER_SIZE+HEAD_SIZE+GAP1_SIZE/*LINE*/
#define CONTENT_WIDTH	320-2*BORDER_SIZE-HEAD_SIZE-GAP1_SIZE

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//self.hidesBottomBarWhenPushed = YES;
	self.navigationItem.title = @"weibodetail";
	
	UIBarButtonItem *timeline = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply
																				target:self action:@selector(backtoTimeLine)];
	self.navigationItem.leftBarButtonItem = timeline;
	[timeline release];
	
	//UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
	//																	  target:self action:@selector(loadStatusToView)];
	//self.navigationItem.rightBarButtonItem = reloadButton;
	//[reloadButton release];
	
	//test
	UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 380)];
	 scrollView.directionalLockEnabled = YES;
	 scrollView.pagingEnabled = YES;
	 scrollView.backgroundColor = [UIColor whiteColor];
	 scrollView.showsVerticalScrollIndicator = YES;
	 scrollView.showsHorizontalScrollIndicator = YES;
	 //scrollView.delegate = self;
	
	
	//定义其它控件
	NSDictionary *userData = [status objectForKey:@"user"];
	NSDictionary *retweetData = [status objectForKey:@"retweeted_status"];
	//头像
	CGRect rect;
	
	rect = CGRectMake(BORDER_SIZE, BORDER_SIZE, HEAD_SIZE, HEAD_SIZE);
	UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect];
	[imageView setTag:HEAD_TAG];
	
	//下载，走着->
//	ImageDLoader *imageDown = [[ImageDLoader alloc]init];
	NSString *headUrl = [userData objectForKey:@"profile_image_url"];
//	[imageDown getImageFromURL:headUrl UiImage:imageView delegate:nil];
//	[imageDown release];
	 
    //[[XImageLoader shareXImageLoader] setImageWithURL:headUrl toView:imageView];
    [imageView setImageViewWithUrl:headUrl placeholderImage:nil];
    
	[scrollView addSubview: imageView];
	[imageView release];
	
	//昵称
	rect = CGRectMake(BORDER_SIZE+HEAD_SIZE+GAP1_SIZE, BORDER_SIZE, NICK_WIDTH, NICK_HEIGHT);
	UILabel *nickLable  = [[UILabel alloc] initWithFrame:rect];
	[nickLable setTag:NICK_TAG];
	nickLable.text = [userData objectForKey:@"screen_name"];
	nickLable.textAlignment = UITextAlignmentLeft;
	[scrollView addSubview: nickLable];
	[nickLable release];
	
	CGFloat statusHeight = STATUS_YOFFSET;
	//微博内容
	rect = CGRectMake(BORDER_SIZE, STATUS_YOFFSET, CONTENT_WIDTH, 0);
	UILabel *statusLabel = [[UILabel alloc] initWithFrame:rect];
	statusLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
	//contentlable.textAlignment = UILineBreakModeClip;
	statusLabel.highlightedTextColor = [UIColor whiteColor];
	statusLabel.opaque = NO;
	[statusLabel setTag:STATUS_TAG];
	statusLabel.numberOfLines = 0;
	statusLabel.text = [status objectForKey:@"text"];
	[statusLabel sizeToFit]; 
	statusHeight += statusLabel.frame.size.height;
	[scrollView addSubview:statusLabel];
	[statusLabel release];
	statusHeight += GAP1_SIZE;
	
	//图片
	NSString *thumbUrl = [status objectForKey:@"bmiddle_pic"];
	if (thumbUrl!=nil&&[thumbUrl length]>0) {
		rect = CGRectMake(8*GAP1_SIZE, statusHeight, THUMB_SIZE, THUMB_SIZE);
		UIImageView *thumbImage = [[UIImageView alloc]initWithFrame:rect];
		//[thumbImage sizeToFit];
		//statusHeight+=thumbImage.frame.size.height;
		statusHeight+=THUMB_SIZE;
		statusHeight+=GAP1_SIZE*2;
		thumbImage.contentMode = UIViewContentModeScaleToFill; 
		
//		//下载，走着->
//		ImageDLoader *imageDown = [[ImageDLoader alloc]init];
//		[imageDown getImageFromURL:thumbUrl UiImage:thumbImage delegate:nil];
//		[imageDown release];
//		[scrollView addSubview:thumbImage];
//		[thumbImage release];
	}
	
	CGFloat retweetHeight = 0;
	//==转发区域==//
	if(retweetData != nil)
	{
		retweetHeight += GAP1_SIZE;
		//retweetView
		rect = CGRectMake(BORDER_SIZE, statusHeight, 0, CONTENT_WIDTH);
		UIView *retweetView= [[UIView alloc] initWithFrame:rect];
		retweetView.backgroundColor = [UIColor lightGrayColor];
		
		//转发内容
		rect = CGRectMake(GAP1_SIZE, GAP1_SIZE, CONTENT_WIDTH-GAP1_SIZE, 0);
		UILabel *retweet_statusLabel = [[UILabel alloc] initWithFrame:rect];
		retweet_statusLabel.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
		//contentlable.textAlignment = UILineBreakModeClip;
		retweet_statusLabel.highlightedTextColor = [UIColor whiteColor];
		retweet_statusLabel.backgroundColor = [UIColor clearColor];
		retweet_statusLabel.opaque = NO;
		//[retweet_statusLabel setTag:RETWEET_STATUS];
		retweet_statusLabel.numberOfLines = 0;
		//设置内容
		//retweet_statusLabel.text = [retweetData objectForKey:@"text"];
		retweet_statusLabel.text = [NSString stringWithFormat:@"%@: %@",[[retweetData objectForKey:@"user"]objectForKey:@"screen_name"]
											,[retweetData objectForKey:@"text"]];
		[retweet_statusLabel sizeToFit];
		retweetHeight += retweet_statusLabel.frame.size.height;
		retweetHeight+=GAP1_SIZE;
		
		
		//thumb
		NSString *thumbUrl0 = [retweetData objectForKey:@"bmiddle_pic"];
		if (thumbUrl0!=nil&&[thumbUrl0 length]>0) {
			
			//重设thumb区域
			
			rect = CGRectMake(GAP1_SIZE, retweetHeight+GAP1_SIZE, THUMB_SIZE, THUMB_SIZE);
			//[retweet_statusThumb setFrame:rect];
			UIImageView *retweet_statusThumb= [[UIImageView alloc] initWithFrame:rect];
			
			[retweetView addSubview:retweet_statusThumb];
			[retweet_statusThumb release];
		
			
			retweetHeight += GAP1_SIZE;
			retweetHeight += THUMB_SIZE;
			//下载，走着->
//			ImageDLoader *imageDown = [[ImageDLoader alloc]init];
//			[imageDown getImageFromURL:thumbUrl0 UiImage:retweet_statusThumb delegate:nil];
//			[imageDown release];
		}
		
		
		//retweetview区域
		rect = CGRectMake(BORDER_SIZE, statusHeight,CONTENT_WIDTH, retweetHeight);
		[retweetView setFrame:rect];
		
		[retweetView addSubview:retweet_statusLabel];
		[retweet_statusLabel release];
		
		[scrollView addSubview:retweetView];
		[retweetView release];

	}
	
	//设置scrollview的容积
	CGSize newSize = CGSizeMake(self.view.frame.size.width,  statusHeight+retweetHeight+BORDER_SIZE*4);
	[scrollView setContentSize:newSize];	
	[self.view addSubview:scrollView];
	[scrollView release];
	
}

-(void) backtoTimeLine{
	
	[self.navigationController popViewControllerAnimated:YES];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)dealloc {
	[status release];
    [super dealloc];
}


@end
